import url from './baseUrl.js' //环境

let token = ' ';

function service(options = {}) {
	token = uni.getStorageSync('token')
	options.url = `${url}${options.url}`;
	//配置请求头
	options.header = {
		'token': token,
	};

	return new Promise((resolved, rejected) => {
		//成功
		options.success = (res) => {
			if (Number(res.data.code) == 10000 || Number(res.data.code) == 1 || Number(res.data.code) ==
				10001) { //请求成功
				resolved(res.data, res.code, res.msg);
			} else {
				uni.showToast({
					icon: 'none',
					duration: 3000,
					title: `${res.data.msg}`
				});

				if (res.data.msg == '请登录员工账号' || res.data.msg == '请登录服务商账号' || res.data.msg == '请登录商家账号' ||
					res.data.msg == '请登录后操作') {
					setTimeout(() => {
						uni.redirectTo({
							url: "/pages/login/index"
						})
						uni.clearStorageSync()
					}, 1500)
				}
				rejected(res.data); //错误
			}

		}
		//错误
		options.fail = (err) => {
			rejected(err); //错误
		}
		uni.request(options);

	});
}
export default service;